<template>
<div class="meal_ticket">
    <h1 style="text-align:center;font-size: 60px;line-height: 80px;color:red">扫条码、领餐券<br />（每人一张，当日有效，丢失不补）</h1>
    <el-form class="demo-ruleForm" :model="mealTicket" ref="publicMealTicketSubmitForm" @submit.native.prevent>
        <el-form-item label="" prop="donorNo" :rules="[
      { required: true, message: '不能为空'}
    ]">
            <el-input class="donorNo-input" placeholder="请扫描献血浆者编号" v-model.number="mealTicket.donorNo" v-focus @keyup.enter.native="publicMealTicketSubmit('publicMealTicketSubmitForm')">
                <template slot="prepend">
                    <div style="font-size:30px;">请扫描献血浆者编号</div>
                </template>
            </el-input>
        </el-form-item>
    </el-form>
    <div class="publicMealTicketCount">
        <el-tag style="font-size: 30px;" :model="mealTicket.count">已发{{mealTicket.count}}</el-tag>
    </div>
</div>
</template>

<script>
import {
    isRegister,
    publicMealTicketCount,
    publicMealTicket
} from "@/api/plasma";
import {
    Notification,
    MessageBox,
    Message,
    Loading
} from 'element-ui'
import Vue from 'vue';
import { io } from "socket.io-client";
import moment from 'moment';

Vue.prototype.indicators = undefined;

export default {
    name: "plasmaMealTicket",
    data() {
        return {
            mealTicket: {
                donorNo: '',
                count: 0
            },
            mealTicketTemp :{
                donorNo:'',
                count:0
            },
            socket:null,
            printer1:"Canon iR C3222L UFR II",
            printer2:"HUAWEI PixLab X1-1843-ipv4",
            printer3:"TSC TTP-247"
        }
    },
    created: function () {
        this.publicMealTicketCount();
        //需在客户端安装hiprint
        this.socket = io("http://localhost:17521", {
            transports: ["websocket"],
            auth: {
            token: "vue-plugin-hiprint", // 在此处填入你 client 设置的 token，缺省可留空
            }
        });
        this.socket.on("connect", () => {
            globalThis.connect = true;
            // TODO: Do something for your project
        })
        if (globalThis.connect == false) {
            alert("未连接静默打印客户端！")
            window.open("hiprint://")
            
        } 
        this.socket.on("clientInfo", (clientInfo) => {
            globalThis.clientInfo = clientInfo;
            console.log(globalThis.clientInfo)
        });

        this.socket.on("printerList", (printerList) => {
            globalThis.printerList = printerList;
            console.log(globalThis.printerList)
        })
        
    },
    methods: {
        publicMealTicketSubmit(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) { //验证通过后
                    //this.mealTicketTemp.donorNo = this.mealTicket.donorNo;
                    isRegister(this.mealTicket.donorNo).then(res => {
                        publicMealTicket(this.mealTicket.donorNo).then(res => {
                            Message({
                                message: '发放成功',
                                type: 'success'
                            })
                            this.publicMealTicketCount()
                            this.print();
                        })
                        this.$refs[formName].resetFields();
                        /*
                        MessageBox.confirm("确定发放餐券吗？", "系统提示").then(() => {
                            publicMealTicket(this.mealTicket.donorNo).then(res => {
                                Message({
                                    message: '发放成功',
                                    type: 'success'
                                })
                                this.publicMealTicketCount()
                            })
                            this.$refs[formName].resetFields();
                        }).catch(() => {
                            this.$refs[formName].resetFields();
                            Message({
                                message: '已取消发放',
                                type: 'info'
                            })
                        })
                        */
                    }).catch(() => {
                            this.$refs[formName].resetFields();
                        })

                } else {
                    this.publicMealTicketCount()
                    return false;
                }
            });
        },
        print() {
            console.log("start print");
            if (globalThis.connect) {
                    /**
                     * @description: 打印 html 字符串
                     * @param {String} html 打印的html字符串
                     * @param {String|number} templateId vue-plugin-hiprint 中的模板id，你可以自定义传入一个 Id，用于回调 success/error 判断
                     * @param {String} printer 打印机名称 printer.name 可为空，缺省默认使用设备默认打印机
                     * @param {pageSize} pageSize 打印纸张大小 { height: 80 * 1000, width: 60 * 1000 } 可为空，缺省默认使用打印机默认纸张
                     * @description: 其他参数参考 默认打印参数说明
                     */
                    const printer = this.printer3;
                    console.log(printer)

                    const momentObj = moment();
                    const dateString = momentObj.format("yyyy年MM月DD日")

                    //this.socket.emit("news", { html:"   ", templateId:null, printer:printer });
                    
                    const html = "<div style='text-align:center;font-size:14px'><h3>餐券</h3><p>"+ dateString +"</p><p>" + this.mealTicketTemp.donorNo +"</p><p>当日有效，丢失不补</p><p>每人每日仅限1张</p></div>";
                    const templateId = "";
                    const pageSize = { height: 50 * 1000, width: 20 * 1000,copies: 1 }
                    
                    this.socket.emit("news", { html, client: null, templateId, printer, pageSize })

                    console.log(pageSize)
                } else {
                    alert("未连接静默打印客户端！")
                    window.open("hiprint://")
                }
    },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        publicMealTicketCount() {
            console.log("获取已发餐券数量")
            publicMealTicketCount().then(res => {
                this.mealTicket.count = res.msg
            });
        }
    },
    directives: {
        focus: {
            inserted: function (el) {
                el.querySelector('input').focus()
            }
        }
    }
};
</script>

<style lang="scss">
.el-input__inner {height:100px;font-size:30px}
.el-message__content {font-size:30px;text-align: center;width:100%;line-height:50px;}
.meal_ticket {
    .donorNo-input .el-input__inner {
        height: 100px;
        font-size: 30px
    }

    .cell {
        text-align: center;
    }

    .publicMealTicketCount {
        text-align: center;

        .el-tag {
            height: 50px;
            line-height: 50px
        }
    }

    .header {
        background-color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    margin-top:100px;
    padding:20px;
    justify-content: center;
    align-items: center;
    height: 100%;
    /*background-image: url("../assets/images/login-background.jpg");*/
    background-size: cover;
}

.title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
}

.el-meal_ticket-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
}

.meal_ticket-code-img {
    height: 38px;
}
</style>
